{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font>Superposition</h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2 </h3>\n",
    "\n",
    "We have a quantum system with four states: $\\ket{00}$, $ \\ket{01} $, $\\ket{10}$, and $ \\ket{11} $.\n",
    "\n",
    "We can also say that our system has two qubits.\n",
    "\n",
    "\n",
    "Suppose that the system is in the following state:\n",
    "\n",
    "$ \\myrvector{ \\dfrac{ 1 }{  \\sqrt{3} - \\sqrt{ 5 + 2\\sqrt{6}} } \\\\ \\\\ \\dfrac{1}{ \\sqrt{3} - \\sqrt{ 7 + 2\\sqrt{12} } } \\\\ \\\\\n",
    " \\dfrac{  1 }{ \\sqrt{5} - \\sqrt{  13 + 2\\sqrt{40} }  } \\\\ \\\\ \\dfrac{1}{ \\sqrt{ 7 } - \\sqrt{ 15 + 2 \\sqrt{56} } } }.  $\n",
    " \n",
    " Find the probability of observing the system in state $\\ket{00}$, $ \\ket{01} $, $\\ket{10}$, or $ \\ket{11} $.\n",
    " \n",
    " You may write a function to calculate the dominator of each fraction automatically, where its value is determined by three values $a$, $ b$, and $ c $ by assuming the form $  \\sqrt{a} - \\sqrt{b + 2 \\sqrt{c} } $.\n",
    " \n",
    " Verify that the total probability is 1 (or almost 1)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def square_roots(a,b,c):\n",
    "    # we iteratively calculate the expression with many square roots\n",
    "    # we start with c and continue with b and a\n",
    "    result = c**0.5 # square root of c\n",
    "    result = 2 * result # 2*sqrt(c)\n",
    "    result = result + b # b + 2*sqrt(c)\n",
    "    result = result**0.5 # square root\n",
    "    result = a**0.5 - result\n",
    "    return result\n",
    "\n",
    "quantum_state =[\n",
    "    square_roots(3,5,6)**(-1),\n",
    "    square_roots(3,7,12)**(-1),\n",
    "    square_roots(5,13,40)**(-1),\n",
    "    square_roots(7,15,56)**(-1),\n",
    "] # this is our quantum state\n",
    "\n",
    "# print the quantum state\n",
    "print(quantum_state)\n",
    "\n",
    "print()\n",
    "print(\"The probability of observing the states 00, 01, 10, 11:\")\n",
    "total_probability = 0\n",
    "for i in range(len(quantum_state)):\n",
    "    current_probability = quantum_state[i]**2 # square of the amplitude\n",
    "    print(current_probability)\n",
    "    total_probability = total_probability + current_probability\n",
    "\n",
    "print()\n",
    "print(\"total probability is\",total_probability)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task3\"></a>\n",
    "<h3> Task 3 </h3>\n",
    "\n",
    "Create a quantum ciruit with 5 qubits.\n",
    "\n",
    "Apply h-gate (Hadamard operator) to each qubit.\n",
    "\n",
    "Apply z-gate ($Z$ operator) to randomly picked qubits. (e.g., $ mycircuit.z(qreg[i]) $)\n",
    "\n",
    "Apply h-gate to each qubit \n",
    "\n",
    "Measuere each qubit.\n",
    "\n",
    "Execute your program 1000 times.\n",
    "\n",
    "Compare the outcomes of the qubits affected by z-gates, and the outcomes of the qubits not affected by z-gates.\n",
    "\n",
    "Does z-gate change the outcome?\n",
    "\n",
    "Why?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "# import randrange for random choices\n",
    "from random import randrange\n",
    "\n",
    "number_of_qubit = 5\n",
    "\n",
    "# define a quantum register with 5 qubits\n",
    "qreg =  QuantumRegister(number_of_qubit)\n",
    "\n",
    "# define a classical register with 5 bits\n",
    "creg = ClassicalRegister(number_of_qubit)\n",
    "\n",
    "# define our quantum circuit\n",
    "mycircuit = QuantumCircuit(qreg,creg)\n",
    "\n",
    "# apply h-gate to all qubits\n",
    "for i in range(number_of_qubit):\n",
    "    mycircuit.h(qreg[i])\n",
    "\n",
    "# apply z-gate randomly picked qubits\n",
    "for i in range(number_of_qubit):\n",
    "    if randrange(2) == 0: # the qubit with index i is picked to apply z-gate\n",
    "        mycircuit.z(qreg[i])\n",
    "        \n",
    "# apply h-gate to all qubits\n",
    "for i in range(number_of_qubit):\n",
    "    mycircuit.h(qreg[i])\n",
    "    \n",
    "    \n",
    "# measure all qubits\n",
    "mycircuit.measure(qreg,creg)\n",
    "\n",
    "print(\"Everything looks fine, let's continue ...\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# draw the circuit\n",
    "\n",
    "drawer(mycircuit)\n",
    "# reexecute me if you DO NOT see the circuit diagram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# execute the circuit 1000 times in the local simulator\n",
    "\n",
    "job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=1000)\n",
    "counts = job.result().get_counts(mycircuit)\n",
    "for outcome in counts: # print the reverse of the outcome\n",
    "    reverse_outcome = ''\n",
    "    for i in outcome:\n",
    "        reverse_outcome = i + reverse_outcome\n",
    "    print(reverse_outcome,\"is observed\",counts[outcome],\"times\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We start in state $ \\ket{0} $ in each qubit.\n",
    "\n",
    "If a qubit is affected by z-gate, then its final value is changed to $ \\ket{1} $.\n",
    "\n",
    "If a qubit is not affected by z-gate, then its final value is $ \\ket{0} $."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
